package com.lbj.googleplay.googleplay100.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lbj.googleplay.googleplay100.ui.view.LoadingPage;
import com.lbj.googleplay.googleplay100.utils.UIUtils;

import java.util.ArrayList;

public abstract class BaseFragment extends Fragment {

    private LoadingPage mLoadingPage;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

//        TextView view = new TextView(UIUtils.getContext());
//         view.setTextSize(30);
//         view.setTextColor(Color.BLACK);
//         view.setText(getClass().getSimpleName());

            mLoadingPage = new LoadingPage(UIUtils.getContext()){

                @Override
                public View onCreateSuccessView() {
                    return BaseFragment.this.onCreateSuccessView();
                }

                @Override
                public ResultState onLoad() {
                    return BaseFragment.this.onLoad();
                }
            };

          return mLoadingPage;
    }
    //加载数据
    public void loadData(){

        if (mLoadingPage!=null){
            mLoadingPage.loadData();
        }

    }
    //校验网络数据
    public LoadingPage.ResultState check(Object obj){
        if (obj!=null){
            if (obj instanceof ArrayList){//判断是否是集合
                ArrayList list = (ArrayList)obj;
                if (list.isEmpty()){
                    return LoadingPage.ResultState.STATE_EMPTY;
                }else {
                    return LoadingPage.ResultState.STATE_SUCCESS;
                }

            }

        }
        return LoadingPage.ResultState.STATE_ERROR;
    }
    //加载成功后显示的布局,必须由子类实现
    public abstract View onCreateSuccessView();
    //加载网络数据的,必须由子类实现
    public abstract LoadingPage.ResultState onLoad();



}
